<script setup>
import { ref, onMounted } from 'vue'
import { queryAllApi, addApi, queryByIdApi, updateApi,deleteApi } from '../../api/dept'
import { ElMessage, ElMessageBox } from 'element-plus'
const tableData = ref([])
const search =async () => {
  // const res = await axios.get('https://m1.apifoxmock.com/m1/6329726-6025112-default/depts?apifoxApiId=297300445')
  // if (res.data.code) {
  //   tableData.value = res.data.data;
  // }
  const res = await queryAllApi();
  if (res.code) {
    tableData.value = res.data;
  }
}
onMounted(() => {
  search(); 
})
// 新增部门
const dialogFormVisible = ref(false);
const formTitle = ref('');
const dept = ref({
  name: ''
})
const addDept = () => {
  dialogFormVisible.value = true; 
  formTitle.value = '新增部门';
  dept.value = {
    name: ''
  }
  if (deptFormRef.value) {
    deptFormRef.value.resetFields(); // 重置表单字段
    
  }
 
}
const save = async () => {
  if(!deptFormRef.value) return
  deptFormRef.value.validate(async (valid) => {
    if (valid) {
      let res; 
      if(dept.value.id) {
        res = await updateApi(dept.value);
      }else {
         res = await addApi(dept.value);
      }
      if (res.code) {
        ElMessage.success('新增成功');
        dialogFormVisible.value = false;
        search();
      } else {
        ElMessage.error(res.message);
      }
    }else {
      ElMessage.error('表单验证失败，请检查输入');
    }
  });
  
 
}
const rules = ref({
  name: [
    { required: true, message: '请输入部门名称', trigger: 'blur' }
    , { min: 2, max: 10, message: '长度在 2 到 10 个字符之间', trigger: 'blur' }
  ]
})
const deptFormRef = ref();

// 编辑部门
const editDept = async (id) => {
 const res = await queryByIdApi(id);
 if (res.code) {
    dialogFormVisible.value = true; 
    formTitle.value = '编辑部门';
    dept.value = res.data;
    if (deptFormRef.value) {
      deptFormRef.value.resetFields(); // 重置表单字段
    }
  } else {
    ElMessage.error(res.message);
  }
}
// 删除部门
const delDept = async (id) => {
  ElMessageBox.confirm(
    '此操作将永久删除该部门, 是否继续?',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(async () => {
      const res = await deleteApi(id);
      if (res.code) {
        ElMessage.success('删除成功');
        search();
      }
    })
    .catch(() => {
      ElMessage.info('已取消删除');
    })
  

}

</script>

<template>
  <h1>部门管理</h1>
  <div class="container">
    <el-button type="primary" @click="addDept">+新增部门</el-button>
  </div>
  <div class="container">
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column type="index" label="序号" width="100" align="center"></el-table-column>
      <el-table-column prop="name" label="部门名称" width="260" align="center"></el-table-column>
      <el-table-column prop="updateTime" label="最后操作时间" width="300" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" size="small" @click="editDept(scope.row.id)"><el-icon>
              <EditPen />
            </el-icon>编辑</el-button>
          <el-button type="danger" size="small" @click="delDept(scope.row.id)"><el-icon>
              <Delete />
            </el-icon>删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <el-dialog v-model="dialogFormVisible" :title="formTitle" width="500">
    <el-form :model="dept" :rules="rules" ref="deptFormRef" >
      <el-form-item label="部门名称" label-width="80px" prop="name">
        <el-input v-model="dept.name"  auto-complete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="save">
          确定
        </el-button>
      </div>
    </template>
  </el-dialog>


</template>

<style scoped>
.container {
  margin: 10px 0;
}

</style>
